package org.hanbo.code.repository.action;

import java.util.ArrayList;
import java.util.List;

import org.hanbo.code.repository.persistence.entity.CodeSnippet;

public class CodeSnippetListItem
{
	private String submissionTime;
	private String subject;
	private String author;
	private String codeSnippetUrl;

	public void setSubmissionTime(String submissionTime)
	{
		this.submissionTime = new String(submissionTime);
	}

	public String getSubmissionTime()
	{
		return submissionTime;
	}

	public void setSubject(String subject)
	{
		this.subject = new String(subject);
	}

	public String getSubject()
	{
		if (subject.length() <= 54)
		{
			return subject;
		}
		else
		{
			String x = subject.substring(0, 54);
			x = x + "...";
			return x;
		}
	}

	public void setAuthor(String author)
	{
		this.author = new String(author);
	}

	public String getAuthor()
	{
		return author;
	}

	public void createCodeSnippetLink(long codeSnippetId)
	{
		codeSnippetUrl = "/RetrieveCodeSnippetById?type=CodeSnippet&id=" + codeSnippetId;
	}

	public String getCodeSnippetUrl()
	{
		return codeSnippetUrl;
	}
	
	public static CodeSnippetListItem createCodeSnippetListItem(CodeSnippet snippet)
	{
		CodeSnippetListItem retItem = new CodeSnippetListItem();
		
		retItem.setAuthor(snippet.getAuthorEmail());
		retItem.setSubject(snippet.getSubject());
		retItem.setSubmissionTime(snippet.getSubmissionDateAsString());
		
		retItem.createCodeSnippetLink(snippet.getId().getId());
		
		return retItem;
	}
	
	public static List<CodeSnippetListItem> createListOfCodeSnippetsForTable(List<CodeSnippet> codeSnippets)
	{
		List<CodeSnippetListItem> retListItems = new ArrayList<CodeSnippetListItem>();
		for (CodeSnippet s : codeSnippets)
		{
			CodeSnippetListItem listItem = createCodeSnippetListItem(s);
			
			retListItems.add(listItem);
		}
		
		return retListItems;
	}
}
